<?php
	require_once( "functions/inc_systemFunctions.php" );
	page_header( "Wayne Schlegel Inc. | Product finder", true, "myflight" );
	
	if( !empty( $_GET['location'] ) ){
		$_POST['location'] = $_GET['location'];
	}
	if( empty( $_POST['location'] ) ){
		$_POST['location'] = "";
	}
?>

<table class="iTable">
	<?php
    # show all product-goups to select from top to bottom
    $sql = "SELECT * FROM productCategory ORDER BY description ASC";
    $res = mysql_query( $sql );
    $cnt = 0;
    while( $row = mysql_fetch_array( $res ) ){
    	$cnt++;
    	echo "<tr class=\"moreInformation";
    	if( $cnt == mysql_num_rows( $res ) ){
    		echo " lastItem";
    	}
    	echo "\">";
    	echo "<td><a href=\"product-finder.php?location=" . $row['id'] . "\">" . $row['description'] . " (" . amountOfProductsInLocation( $row['id'] ) . ")</a></td>";
    	echo "</tr>";
    }
    ?>
</table>		


<?php
	if( !empty( $_POST['location'] ) ){
		# get products in selected category
		$sql = "SELECT * FROM product WHERE isActive = 1 AND productCategory_id = " . $_POST['location'];
		$res = mysql_query( $sql );
		if( mysql_num_rows( $res ) == 0 ){
			echo "<p class=\"red\">Sorry. We have no products in the slected category.</p>";
		} else {
			$productCount = 0;
			echo "<table class=\"iTable\">";
			while( $row = mysql_fetch_array( $res ) ){
				$productCount++;
				echo "<tr class=\"moreInformation";
				if( $productCount == mysql_num_rows( $res ) ){
					echo " lastItem";
				}
				echo "\">";
				echo "<td><a href=\"product-details.php?pid=" . $row['id'] . "&backLink=" . base64_encode( "/product-finder.php?location=" . $_POST['location'] ) . "\"><span style=\"width: 40px; height: 40px; overflow: hidden; line-height: 40px;\"><image id=\"productImage\" width=\"35\" src=\"/functions/getBinaryData.php?blobId=" . $row['productImages_id'] . "\" align=\"absmiddle\" /></span> " . $row['marketingName'] . "</a></td>";
				echo "<tr>\n";
			}
			echo "</table>";
		}
	}
?>

<?php
	page_footer();
?>